package cn.wk.quartz.job;

import cn.wk.basic.constant.ConstantPethome;
import cn.wk.basic.constant.PayConstant;
import cn.wk.order.domain.ProductOrder;
import cn.wk.order.mapper.ProductOrderMapper;
import cn.wk.order.service.IProductOrderService;
import cn.wk.pay.domain.PayBill;
import cn.wk.pay.mapper.PayBillMapper;
import cn.wk.quartz.domain.QuartzJobInfo;
import cn.wk.quartz.util.QuartzUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

public class Oreder implements Job {
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    /**
     * 定时任务
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取jobDataMap参数
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        //获取参数
        QuartzJobInfo quartzJobInfo = (QuartzJobInfo) mergedJobDataMap.get("params");
        //获取orderSn
        Map<String, Object> params = quartzJobInfo.getParams();
        //根据相对应的服务类型
        switch (quartzJobInfo.getType()){
            case PayConstant.BUSINESSTYPE_PRODUCT:
                //获取服务订单编号
                String orderSn = (String) params.get("orderSn");
                //根据订单编号去查询服务订单对象
                ProductOrder productOrder = productOrderMapper.findByOrderSn(orderSn);
                //设置最后时间
                productOrder.setLastConfirmTime(new Date());
                //更改状态
                productOrder.setState(ConstantPethome.CANCLE);
                //数据库更新
                productOrderMapper.update(productOrder);
                //根据订单编号查询支付订单
                PayBill payBill = payBillMapper.findByOrderSn(orderSn);
                payBill.setUpdateTime(new Date());
                payBill.setState(ConstantPethome.CANCLE);
                payBillMapper.update(payBill);
                break;
            case PayConstant.BUSINESSTYPE_ADOPT:
                break;
            case PayConstant.BUSINESSTYPE_GOODS:
                break;
        }
    }
}
